/*
 * Filename:    IndicationController.java
 * Copyright:   2014 中国平安保险（集团）股份有限公司 版权所有
 * Company:     平安科技养老险及健康险系统开发部
 * Create at:   2014年10月17日
 */
package com.paic.mhis.hcpms.quota.settlement.web.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.paic.mhis.core.auth.biz.service.UserDetails;
import com.paic.mhis.hcpms.quota.settlement.biz.service.IndicationService;
import com.paic.mhis.hcpms.quota.settlement.biz.service.KpiRegionCodeService;
import com.paic.mhis.hcpms.quota.settlement.common.Constants;
import com.paic.mhis.hcpms.quota.settlement.dto.DateDTO;
import com.paic.mhis.hcpms.quota.settlement.dto.IndicationBeanDTO;
import com.paic.mhis.hcpms.quota.settlement.dto.IndicationQueryParameterDTO;
import com.paic.mhis.hcpms.quota.settlement.dto.IndicationRegionOrgDTO;
import com.paic.mhis.hcpms.quota.settlement.dto.IndicationRegionOrgPageBean;
import com.paic.mhis.hcpms.quota.settlement.dto.IndicationTitleDTO;
import com.paic.mhis.hcpms.quota.settlement.web.vo.IndicationDepartVO;
import com.paic.pafa.appclient.annotation.BizServicesProxy;

@Controller
@RequestMapping("/indication/")
public class IndicationController {

	private static final Logger logger = Logger.getLogger(IndicationController.class);
	@BizServicesProxy(beanName = "indicationServiceImpl")
	private IndicationService indicationService;
	@BizServicesProxy(beanName = "kpiRegionCodeService")
	private KpiRegionCodeService kpiRegionCodeService;

	public String[] str = { "名称", "7月", "8月", "9月", "10月", "11月", "12月", "1月", "2月", "3月", "4月", "5月", "6月", "同比", "环比" };

	public void exportRegionExcel(String excelName, List<IndicationBeanDTO> list, DateDTO dateDto,
			HttpServletResponse response) throws IOException {

		str = getDateStr(dateDto);
		Workbook excel = null;
		try {
			excel = new HSSFWorkbook();
			Sheet sheet = excel.createSheet(excelName);
			Row row = sheet.createRow(0);
			// 设置Excel头
			int j = 0;
			for (String key : str) {
				row.createCell(j).setCellValue(key);
				sheet.setColumnWidth(j, 4000);
				j++;
			}
			// 设置Excel内容
			if (null != list) {
				for (int i = 0; i < list.size(); i++) {
					IndicationBeanDTO dto = list.get(i);
					row = sheet.createRow(i + 1);
					getCellValue(row, dto, dateDto.getStart(), dateDto.getEnd());

				}
			}

			initResponse(response, excelName);
			excel.write(response.getOutputStream());
		} catch (IOException e) {
			logger.error("导出Excel是发生异常,异常原因:" + e.getMessage());
			e.printStackTrace();
		} finally {
			response.getOutputStream().flush();
			response.getOutputStream().close();
		}
	}

	/**
	 * 根据开始日期以及结束日期 获取excel行数据
	 * 
	 * @param row
	 * @param dto
	 * @param startDate
	 * @param endDate
	 */
	private void getCellValue(Row row, IndicationBeanDTO dto, String startDate, String endDate) {
		if (StringUtils.hasText(startDate) && StringUtils.hasText(endDate)) {
			String[] startDates = startDate.split("-");
			String[] endDates = endDate.split("-");
			int startYear = Integer.parseInt(startDates[0]);
			int endYear = Integer.parseInt(endDates[0]);
			int startMonth = Integer.parseInt(startDates[1]);
			int endMonth = Integer.parseInt(endDates[1]);
			row.createCell(0).setCellValue(dto.getName());
			int i = 0;
			for (; i < 12; i++) {
				// 超过12则是下一年的月份
				if (startMonth > 12) {
					startMonth = 1;
					startYear = startYear + 1;
				}
				// 判断如果超出结束日期则跳出循环
				if (startYear == endYear) {
					if (startMonth > endMonth) {
						break;
					}
				}
				// 一月份
				if (Constants.first == startMonth) {
					row.createCell(i + 1).setCellValue(dto.getFirst());
				} else if (Constants.second == startMonth) {
					row.createCell(i + 1).setCellValue(dto.getSecond());
				} else if (Constants.third == startMonth) {
					row.createCell(i + 1).setCellValue(dto.getThird());
				} else if (Constants.fourth == startMonth) {
					row.createCell(i + 1).setCellValue(dto.getFourth());
				} else if (Constants.fifth == startMonth) {
					row.createCell(i + 1).setCellValue(dto.getFifth());
				} else if (Constants.sixth == startMonth) {
					row.createCell(i + 1).setCellValue(dto.getSixth());
				} else if (Constants.seventh == startMonth) {
					row.createCell(i + 1).setCellValue(dto.getSeventh());
				} else if (Constants.eighth == startMonth) {
					row.createCell(i + 1).setCellValue(dto.getEighth());
				} else if (Constants.ninth == startMonth) {
					row.createCell(i + 1).setCellValue(dto.getNinth());
				} else if (Constants.tenth == startMonth) {
					row.createCell(i + 1).setCellValue(dto.getTenth());
				} else if (Constants.eleventh == startMonth) {
					row.createCell(i + 1).setCellValue(dto.getEleventh());
				} else if (Constants.twelfth == startMonth) {
					row.createCell(i + 1).setCellValue(dto.getTwelfth());
				}
				startMonth++;
			}
			row.createCell(i + 1).setCellValue(dto.getYoy());
			row.createCell(i + 2).setCellValue(dto.getMom());
		}
	}

	private String[] getDateStr(DateDTO dateDto) {
		String startDate = dateDto.getStart();
		int startYear = Integer.parseInt(startDate.substring(0, 4));
		int startMonth = Integer.parseInt(startDate.substring(5));
		String endDate = dateDto.getEnd();
		int endYear = Integer.parseInt(endDate.substring(0, 4));
		int endMonth = Integer.parseInt(endDate.substring(5));

		int diffDate = (endYear - startYear) * 12 + endMonth - startMonth + 1;

		String[] str = new String[3 + diffDate];
		str[0] = "名称";
		for (int i = 0; i < diffDate; i++) {
			if (startMonth > 12) {
				startMonth = 1;
				startYear++;
			}
			String tepmDate = startYear + "年" + startMonth + "月";
			str[i + 1] = tepmDate;
			startMonth++;

		}
		str[str.length - 2] = "同比";
		str[str.length - 1] = "环比";
		return str;
	}

	private void initResponse(HttpServletResponse response, String name) {
		try {
			response.addHeader("Pragma", "no-cache");
			response.addHeader("Cache-Control", "no-cache");
			response.reset();
			response.setContentType("APPLICATION/OCTET-STREAM");
			String fileName = new String((name).getBytes("utf-8"), "ISO8859-1") + ".xls";
			response.setHeader("content-disposition", "attachment;fileName=\"" + fileName + "\"");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}

	public void exportLevelOrgExcel(String excelName, List<IndicationRegionOrgDTO> list, DateDTO dateDto,
			HttpServletResponse response) throws IOException {

		Workbook excel = null;
		try {
			excel = new HSSFWorkbook();
			Sheet sheet = excel.createSheet(excelName);
			Row row = sheet.createRow(0);
			// 设置Excel头
			int j = 0;
			str = getDateStr(dateDto);
			for (String key : str) {
				row.createCell(j).setCellValue(key);
				sheet.setColumnWidth(j, 4000);
				j++;
			}
			// 设置Excel内容
			if (null != list) {
				for (int i = 0; i < list.size(); i++) {
					IndicationRegionOrgDTO dto = (IndicationRegionOrgDTO) list.get(i);
					row = sheet.createRow(i + 1);
					getOrgCellValue(row, dto, dateDto.getStart(), dateDto.getEnd());

				}
			}

			initResponse(response, excelName);
			excel.write(response.getOutputStream());
		} catch (IOException e) {
			logger.error("导出Excel是发生异常,异常原因:" + e.getMessage());
			e.printStackTrace();
		} finally {
			response.getOutputStream().flush();
			response.getOutputStream().close();
		}
	}

	/**
	 * 根据开始日期以及结束日期 获取excel行数据
	 * 
	 * @param row
	 * @param dto
	 * @param startDate
	 * @param endDate
	 */
	private void getOrgCellValue(Row row, IndicationRegionOrgDTO dto, String startDate, String endDate) {
		if (StringUtils.hasText(startDate) && StringUtils.hasText(endDate)) {
			String[] startDates = startDate.split("-");
			String[] endDates = endDate.split("-");
			int startYear = Integer.parseInt(startDates[0]);
			int endYear = Integer.parseInt(endDates[0]);
			int startMonth = Integer.parseInt(startDates[1]);
			int endMonth = Integer.parseInt(endDates[1]);
			row.createCell(0).setCellValue(dto.getName());
			int i = 0;
			for (; i < 12; i++) {
				// 超过12则是下一年的月份
				if (startMonth > 12) {
					startMonth = 1;
					startYear = startYear + 1;
				}
				// 判断如果超出结束日期则跳出循环
				if (startYear == endYear) {
					if (startMonth > endMonth) {
						break;
					}
				}
				// 一月份
				if (Constants.first == startMonth) {
					row.createCell(i + 1).setCellValue(dto.getFirst());
				} else if (Constants.second == startMonth) {
					row.createCell(i + 1).setCellValue(dto.getSecond());
				} else if (Constants.third == startMonth) {
					row.createCell(i + 1).setCellValue(dto.getThird());
				} else if (Constants.fourth == startMonth) {
					row.createCell(i + 1).setCellValue(dto.getFourth());
				} else if (Constants.fifth == startMonth) {
					row.createCell(i + 1).setCellValue(dto.getFifth());
				} else if (Constants.sixth == startMonth) {
					row.createCell(i + 1).setCellValue(dto.getSixth());
				} else if (Constants.seventh == startMonth) {
					row.createCell(i + 1).setCellValue(dto.getSeventh());
				} else if (Constants.eighth == startMonth) {
					row.createCell(i + 1).setCellValue(dto.getEighth());
				} else if (Constants.ninth == startMonth) {
					row.createCell(i + 1).setCellValue(dto.getNinth());
				} else if (Constants.tenth == startMonth) {
					row.createCell(i + 1).setCellValue(dto.getTenth());
				} else if (Constants.eleventh == startMonth) {
					row.createCell(i + 1).setCellValue(dto.getEleventh());
				} else if (Constants.twelfth == startMonth) {
					row.createCell(i + 1).setCellValue(dto.getTwelfth());
				}
				startMonth++;
			}
			row.createCell(i + 1).setCellValue(dto.getYoy());
			row.createCell(i + 2).setCellValue(dto.getMom());
		}
	}

	/**
	 * 导出科室下的医生
	 * 
	 * @param vo
	 * @param response
	 * @param request
	 */
	@RequestMapping("doctorExport")
	@ResponseBody
	public void doctorExport(@ModelAttribute IndicationDepartVO vo, HttpServletResponse response,
			HttpServletRequest request) {
		UserDetails user = (UserDetails) request.getSession().getAttribute("user");
		String regionCode = this.kpiRegionCodeService.getRegionCode(user.getUserId());
		String excelName = this.getIndicationExcelName(vo.getIndicator());
		String deptCode = vo.getId();

		DateDTO dateDto = new DateDTO();
		dateDto.setStart(vo.getStartDate());
		dateDto.setEnd(vo.getEndDate());
		// 总 住 门
		String visit = Constants.visitType_all_value;
		if (Constants.visitType_all.equals(vo.getDataType())) {
			visit = Constants.visitType_all_value;
		} else if (Constants.visitType_hosp.equals(vo.getDataType())) {
			visit = Constants.visitType_hosp_value;
		} else if (Constants.visitType_out.equals(vo.getDataType())) {
			visit = Constants.visitType_out_value;
		}
		IndicationQueryParameterDTO bean = new IndicationQueryParameterDTO();
		bean.setIndicator(vo.getIndicator());
		bean.setVisit(visit); // 类别
		bean.setDate(dateDto);
		bean.setStart(0);
		bean.setDepart(deptCode);
		bean.setOrgId(vo.getOrgId());
		bean.setRegionCode(regionCode);
		bean.setLimit(100);

		IndicationRegionOrgPageBean returnBean = this.indicationService.queryDoctors(bean);

		List<IndicationRegionOrgDTO> data = returnBean.getData();

		IndicationQueryParameterDTO bean1 = new IndicationQueryParameterDTO();
		bean1.setIndicator(vo.getIndicator());
		bean1.setVisit(visit); // 类别
		bean1.setDate(dateDto);
		bean1.setRegionCode(regionCode);
		bean1.setOrgId(deptCode);

		List<IndicationRegionOrgDTO> list = this.indicationService.getDeptTableBasicData(bean);

		list.addAll(data);

		try {
			this.exportLevelOrgExcel(excelName, list, dateDto, response);
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	/**
	 * 导出机构下的科室信息
	 * 
	 * @param vo
	 * @param response
	 * @param request
	 */
	@RequestMapping("orgExport")
	@ResponseBody
	public void orgExport(@ModelAttribute IndicationDepartVO vo, HttpServletResponse response,
			HttpServletRequest request) {
		UserDetails user = (UserDetails) request.getSession().getAttribute("user");
		String regionCode = this.kpiRegionCodeService.getRegionCode(user.getUserId());

		String orgCode = vo.getOrgId();

		DateDTO dateDto = new DateDTO();
		dateDto.setStart(vo.getStartDate());
		dateDto.setEnd(vo.getEndDate());
		// 总 住 门
		String visit = Constants.visitType_all_value;
		if (Constants.visitType_all.equals(vo.getDataType())) {
			visit = Constants.visitType_all_value;
		} else if (Constants.visitType_hosp.equals(vo.getDataType())) {
			visit = Constants.visitType_hosp_value;
		} else if (Constants.visitType_out.equals(vo.getDataType())) {
			visit = Constants.visitType_out_value;
		}
		IndicationQueryParameterDTO bean = new IndicationQueryParameterDTO();
		bean.setIndicator(vo.getIndicator());
		bean.setVisit(visit); // 类别
		bean.setDate(dateDto);
		bean.setRegionCode(regionCode);
		bean.setStart(0);
		bean.setOrgId(orgCode);
		bean.setLimit(100);

		IndicationRegionOrgPageBean returnBean = this.indicationService.queryDepartments(bean);
		String excelName = this.getIndicationExcelName(vo.getIndicator());
		List<IndicationRegionOrgDTO> data = returnBean.getData();

		IndicationQueryParameterDTO bean1 = new IndicationQueryParameterDTO();
		bean1.setIndicator(vo.getIndicator());
		bean1.setVisit(visit); // 类别
		bean1.setDate(dateDto);
		bean1.setRegionCode(regionCode);
		bean1.setOrgId(orgCode);

		List<IndicationRegionOrgDTO> headList = this.indicationService.getOrgTableBasicData(bean);

		headList.addAll(data);

		try {
			this.exportLevelOrgExcel(excelName, headList, dateDto, response);
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	/**
	 * 导出第一层级别，以及级别下的机构
	 * 
	 * @param vo
	 * @param response
	 * @param request
	 */
	@RequestMapping("regionExport")
	@ResponseBody
	public void regionExport(@ModelAttribute IndicationDepartVO vo, HttpServletResponse response,
			HttpServletRequest request) {
		UserDetails user = (UserDetails) request.getSession().getAttribute("user");
		String regionCode = this.kpiRegionCodeService.getRegionCode(user.getUserId());
		DateDTO dateDto = new DateDTO();
		dateDto.setStart(vo.getStartDate());
		dateDto.setEnd(vo.getEndDate());
		// 总 住 门
		String visit = Constants.visitType_all_value;
		if (Constants.visitType_all.equals(vo.getDataType())) {
			visit = Constants.visitType_all_value;
		} else if (Constants.visitType_hosp.equals(vo.getDataType())) {
			visit = Constants.visitType_hosp_value;
		} else if (Constants.visitType_out.equals(vo.getDataType())) {
			visit = Constants.visitType_out_value;
		}
		IndicationQueryParameterDTO bean = new IndicationQueryParameterDTO();
		bean.setIndicator(vo.getIndicator());
		bean.setVisit(visit); // 类别
		bean.setDate(dateDto);
		bean.setRegionCode(regionCode);

		String excelName = this.getIndicationExcelName(vo.getIndicator());

		// 机构等级
		if (StringUtils.hasText(vo.getLevel()) && !"0".equals(vo.getLevel())) {
			bean.setStart(0);
			bean.setOrgLevel(vo.getLevel());
			bean.setLimit(100);
			IndicationRegionOrgPageBean returnBean = this.indicationService.queryRegionOrgs(bean);
			List<IndicationRegionOrgDTO> data = returnBean.getData();
			try {
				this.exportLevelOrgExcel(excelName, data, dateDto, response);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		// 机构类型
		else if (StringUtils.hasText(vo.getType())) {
			bean.setOrgCategory(vo.getType());
			bean.setStart(0);
			bean.setLimit(100);

			IndicationRegionOrgPageBean returnBean = this.indicationService.queryRegionOrgs(bean);
			List<IndicationRegionOrgDTO> data = returnBean.getData();
			try {
				this.exportLevelOrgExcel(excelName, data, dateDto, response);
			} catch (IOException e) {
				e.printStackTrace();
			}

		} else {
			List<IndicationBeanDTO> list = new ArrayList<IndicationBeanDTO>();
			if (StringUtils.hasText(vo.getOrgCategory())) {
				bean.setOrgCategory(vo.getOrgCategory());
				list = this.indicationService.queryIndicationByOrgCategory(bean);
			} else {
				list = this.indicationService.queryIndication(bean);
			}

			try {
				this.exportRegionExcel(excelName, list, dateDto, response);
			} catch (IOException e) {
				e.printStackTrace();
			}

		}

	}

	/**
	 * 根据指标编码生成excelName
	 * 
	 * @param kpiCode
	 * @return
	 */
	public String getIndicationExcelName(String kpiCode) {
		IndicationTitleDTO bean = this.indicationService.queryIndicationTitleDTOByKpiId(kpiCode);
		String name = bean.getLabel();

		Date date = new Date();
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
		String timeStringValue = format.format(date);

		return name + timeStringValue;

	}

	/**
	 * {indicator=KPI0002, date={start=2013-07, end=2014-06}, org=[], doctor=,
	 * depart=[], orgLevel=2, tmp=0.997758605517447}
	 * 
	 * @param map
	 * @param request
	 * @return
	 */
	@RequestMapping("queryOrgCount.do")
	@ResponseBody
	public List<IndicationBeanDTO> queryOrgCount(@RequestBody Map<String, Object> map, HttpServletRequest request) {

		UserDetails user = (UserDetails) request.getSession().getAttribute("user");
		String regionCode = this.kpiRegionCodeService.getRegionCode(user.getUserId());

		Map<String, Object> parMap = (Map<String, Object>) map.get("date");
		String start = (String) parMap.get("start");
		String end = (String) parMap.get("end");

		String indicator = (String) map.get("indicator");
		String level = (String) map.get("orgLevel");
		String orgCategory = (String) map.get("orgCategory");

		DateDTO dateDto = new DateDTO();
		dateDto.setStart(start);
		dateDto.setEnd(end);
		String type = (String) map.get("dataType");
		// 总 住 门
		String visit = Constants.visitType_all_value;
		if (Constants.visitType_all.equals(type)) {
			visit = Constants.visitType_all_value;
		} else if (Constants.visitType_hosp.equals(type)) {
			visit = Constants.visitType_hosp_value;
		} else if (Constants.visitType_out.equals(type)) {
			visit = Constants.visitType_out_value;
		}
		IndicationQueryParameterDTO bean = new IndicationQueryParameterDTO();
		bean.setIndicator(indicator);// 医保费用占比
		bean.setVisit(visit); //
		bean.setDate(dateDto);
		bean.setRegionCode(regionCode);

		List<IndicationBeanDTO> list = null;
		if (StringUtils.hasText(level)) {
			bean.setLevel(level);
			list = this.indicationService.queryIndication(bean);
		}

		if (StringUtils.hasText(orgCategory)) {
			bean.setOrgCategory(orgCategory);
			list = this.indicationService.queryIndicationByOrgCategory(bean);
		}

		if (!(StringUtils.hasText(level)) && !StringUtils.hasText(orgCategory)) {
			list = this.indicationService.queryIndication(bean);
		}
		return list;
	}

	/**
	 * 获取所有指标的第一级数据,第一级，第二级和第三级别第二级 *
	 * 
	 * @param map
	 * @return
	 */
	@RequestMapping("getOrgIndication.do")
	@ResponseBody
	public List<IndicationBeanDTO> getOrgIndication(@RequestBody Map<String, Object> map, HttpServletRequest request) {

		UserDetails user = (UserDetails) request.getSession().getAttribute("user");
		String regionCode = this.kpiRegionCodeService.getRegionCode(user.getUserId());

		Map<String, Object> paramterMap = (Map<String, Object>) map.get("data");

		Map<String, Object> parMap = (Map<String, Object>) paramterMap.get("date");
		String start = (String) parMap.get("start");
		String end = (String) parMap.get("end");

		String indicator = (String) paramterMap.get("indicator");

		String type = (String) paramterMap.get("dataType");
		// 总 住 门
		String visit = Constants.visitType_all_value;
		if (Constants.visitType_all.equals(type)) {
			visit = Constants.visitType_all_value;
		} else if (Constants.visitType_hosp.equals(type)) {
			visit = Constants.visitType_hosp_value;
		} else if (Constants.visitType_out.equals(type)) {
			visit = Constants.visitType_out_value;
		}
		DateDTO dateDto = new DateDTO();
		dateDto.setStart(start);
		dateDto.setEnd(end);

		IndicationQueryParameterDTO bean = new IndicationQueryParameterDTO();
		bean.setIndicator(indicator);// 医保费用占比
		bean.setVisit(visit); // 类别
		bean.setDate(dateDto);
		bean.setRegionCode(regionCode);

		List<IndicationBeanDTO> list = this.indicationService.queryIndication(bean);

		return list;
	}

	/**
	 * 查询级别下面所有的医院指标信息
	 * 
	 * dataType: "all" date: {start:2013-07, end:2014-06} indicator: "KPI0002"
	 * limit: 20 orgId: null orgLevel: 3 start: 1 tmp: 0.2535156838130206
	 * 
	 * @param map
	 * @return
	 */
	@RequestMapping("getRegionOrgs.do")
	@ResponseBody
	public IndicationRegionOrgPageBean getRegionOrgs(@RequestBody Map<String, Object> map, HttpServletRequest request) {

		UserDetails user = (UserDetails) request.getSession().getAttribute("user");
		String regionCode = this.kpiRegionCodeService.getRegionCode(user.getUserId());

		String indicator = (String) map.get("indicator");

		int limitPage = (Integer) map.get("limit");
		int startPage = (Integer) map.get("start");
		String orgLevel = map.get("orgLevel").toString();

		String orgCategory = (String) map.get("orgCategory");

		int startNum = (startPage - 1) * limitPage;
		int endNum = startNum + limitPage;

		Map<String, Object> parMap = (Map<String, Object>) map.get("date");
		String start = (String) parMap.get("start");
		String end = (String) parMap.get("end");

		DateDTO dateDto = new DateDTO();
		dateDto.setStart(start);
		dateDto.setEnd(end);
		String type = (String) map.get("dataType");
		// 总 住 门
		String visit = Constants.visitType_all_value;
		if (Constants.visitType_all.equals(type)) {
			visit = Constants.visitType_all_value;
		} else if (Constants.visitType_hosp.equals(type)) {
			visit = Constants.visitType_hosp_value;
		} else if (Constants.visitType_out.equals(type)) {
			visit = Constants.visitType_out_value;
		}

		IndicationQueryParameterDTO bean = new IndicationQueryParameterDTO();
		bean.setIndicator(indicator);
		bean.setVisit(visit); //
		bean.setDate(dateDto);
		bean.setStart(startNum);
		bean.setEnd(endNum);

		if (!"0".equals(orgLevel) || StringUtils.hasText(orgLevel)) {
			bean.setOrgLevel(orgLevel);
		}
		if (StringUtils.hasText(orgCategory)) {
			bean.setOrgCategory(orgCategory);
		}
		bean.setRegionCode(regionCode);
		bean.setLimit(limitPage);

		IndicationRegionOrgPageBean returnBean = this.indicationService.queryRegionOrgs(bean);

		return returnBean;
	}

	/**
	 * 根据医院查询医院下的科室指标值
	 * 
	 * @param map
	 * @return
	 */
	@RequestMapping("getDepartments.do")
	@ResponseBody
	public IndicationRegionOrgPageBean getDepartments(@ModelAttribute IndicationDepartVO vo, HttpServletRequest request) {

		UserDetails user = (UserDetails) request.getSession().getAttribute("user");
		String regionCode = this.kpiRegionCodeService.getRegionCode(user.getUserId());

		String indicator = vo.getIndicator();
		int limitPage = vo.getLimit();
		int startPage = vo.getStart();
		String orgId = vo.getOrgId();

		String startDate = vo.getStartDate();
		String endDate = vo.getEndDate();

		DateDTO dateDto = new DateDTO();
		dateDto.setStart(startDate);
		dateDto.setEnd(endDate);

		int startNum = (startPage - 1) * limitPage;
		// int endNum = startNum + limitPage;
		// 总 住 门
		String visit = Constants.visitType_all_value;
		if (Constants.visitType_all.equals(vo.getDataType())) {
			visit = Constants.visitType_all_value;
		} else if (Constants.visitType_hosp.equals(vo.getDataType())) {
			visit = Constants.visitType_hosp_value;
		} else if (Constants.visitType_out.equals(vo.getDataType())) {
			visit = Constants.visitType_out_value;
		}
		IndicationQueryParameterDTO bean = new IndicationQueryParameterDTO();
		bean.setIndicator(indicator);
		bean.setVisit(visit); // 类型
		bean.setDate(dateDto);
		bean.setStart(startNum);
		// bean.setEnd(endNum);
		bean.setOrgId(orgId);
		bean.setRegionCode(regionCode);
		bean.setLimit(limitPage);

		IndicationRegionOrgPageBean returnBean = this.indicationService.queryDepartments(bean);
		return returnBean;
	}

	/**
	 * 获取科室下面的医生
	 * 
	 * @param vo
	 * @param request
	 * @return
	 */
	@RequestMapping("getDoctors.do")
	@ResponseBody
	public IndicationRegionOrgPageBean getDoctors(@ModelAttribute IndicationDepartVO vo, HttpServletRequest request) {

		UserDetails user = (UserDetails) request.getSession().getAttribute("user");
		String regionCode = this.kpiRegionCodeService.getRegionCode(user.getUserId());

		String indicator = vo.getIndicator();
		int limitPage = vo.getLimit();
		int startPage = vo.getStart();
		String deptId = vo.getId();

		int startNum = (startPage - 1) * limitPage;
		// int endNum = startNum + limitPage;

		String startDate = vo.getStartDate();
		String endDate = vo.getEndDate();

		DateDTO dateDto = new DateDTO();
		dateDto.setStart(startDate);
		dateDto.setEnd(endDate);

		// 总 住 门
		String visit = Constants.visitType_all_value;
		if (Constants.visitType_all.equals(vo.getDataType())) {
			visit = Constants.visitType_all_value;
		} else if (Constants.visitType_hosp.equals(vo.getDataType())) {
			visit = Constants.visitType_hosp_value;
		} else if (Constants.visitType_out.equals(vo.getDataType())) {
			visit = Constants.visitType_out_value;
		}

		IndicationQueryParameterDTO bean = new IndicationQueryParameterDTO();
		bean.setIndicator(indicator);
		bean.setVisit(visit); // 类型
		bean.setDate(dateDto);
		bean.setStart(startNum);
		// bean.setEnd(endNum);
		bean.setOrgId(vo.getOrgId());
		bean.setDepart(deptId);
		bean.setRegionCode(regionCode);
		bean.setLimit(limitPage);

		IndicationRegionOrgPageBean returnBean = this.indicationService.queryDoctors(bean);
		return returnBean;
	}

	/**
	 * 根据医院的code,去加载医院,统筹区的信息
	 * 
	 * @param vo
	 * @param request
	 * @return
	 */
	@RequestMapping("getOrgTableBasicData.do")
	@ResponseBody
	public List<IndicationRegionOrgDTO> getOrgTableBasicData(@ModelAttribute IndicationDepartVO vo,
			HttpServletRequest request) {

		UserDetails user = (UserDetails) request.getSession().getAttribute("user");
		String regionCode = this.kpiRegionCodeService.getRegionCode(user.getUserId());

		String indicator = vo.getIndicator();
		String orgId = vo.getOrgId();

		String startDate = vo.getStartDate();
		String endDate = vo.getEndDate();

		DateDTO dateDto = new DateDTO();
		dateDto.setStart(startDate);
		dateDto.setEnd(endDate);

		// 总 住 门
		String visit = Constants.visitType_all_value;
		if (Constants.visitType_all.equals(vo.getDataType())) {
			visit = Constants.visitType_all_value;
		} else if (Constants.visitType_hosp.equals(vo.getDataType())) {
			visit = Constants.visitType_hosp_value;
		} else if (Constants.visitType_out.equals(vo.getDataType())) {
			visit = Constants.visitType_out_value;
		}

		IndicationQueryParameterDTO bean = new IndicationQueryParameterDTO();
		bean.setIndicator(indicator);
		bean.setVisit(visit); // 类型
		bean.setDate(dateDto);
		bean.setRegionCode(regionCode);

		bean.setOrgId(orgId);

		List<IndicationRegionOrgDTO> list = this.indicationService.getOrgTableBasicData(bean);

		return list;

	}

	/**
	 * 根据科室的code去加载统筹区，医院，科室的指标值
	 * 
	 * @param vo
	 * @param request
	 * @return
	 */
	@RequestMapping("getDeptTableBasicData.do")
	@ResponseBody
	public List<IndicationRegionOrgDTO> getDeptTableBasicData(@ModelAttribute IndicationDepartVO vo,
			HttpServletRequest request) {

		UserDetails user = (UserDetails) request.getSession().getAttribute("user");
		String regionCode = this.kpiRegionCodeService.getRegionCode(user.getUserId());

		String indicator = vo.getIndicator();
		String deptCode = vo.getId(); // 科室的code

		String startDate = vo.getStartDate();
		String endDate = vo.getEndDate();

		DateDTO dateDto = new DateDTO();
		dateDto.setStart(startDate);
		dateDto.setEnd(endDate);
		// 总 住 门
		String visit = Constants.visitType_all_value;
		if (Constants.visitType_all.equals(vo.getDataType())) {
			visit = Constants.visitType_all_value;
		} else if (Constants.visitType_hosp.equals(vo.getDataType())) {
			visit = Constants.visitType_hosp_value;
		} else if (Constants.visitType_out.equals(vo.getDataType())) {
			visit = Constants.visitType_out_value;
		}

		IndicationQueryParameterDTO bean = new IndicationQueryParameterDTO();
		bean.setIndicator(indicator);
		bean.setVisit(visit); // 类型
		bean.setDate(dateDto);
		bean.setRegionCode(regionCode); // 统筹区
		bean.setOrgId(vo.getOrgId());
		bean.setDepart(deptCode); // 科室

		List<IndicationRegionOrgDTO> list = this.indicationService.getDeptTableBasicData(bean);

		return list;

	}

	/**
	 * 查询指标页面中的标题
	 * 
	 * @param vo
	 * @param request
	 * @return
	 */
	@RequestMapping("getIndicatorInfo.do")
	@ResponseBody
	public IndicationTitleDTO getIndicatorInfo(@ModelAttribute IndicationDepartVO vo, HttpServletRequest request) {

		String id = vo.getId();

		IndicationTitleDTO bean = this.indicationService.queryIndicationTitleDTOByKpiId(id);
		// bean.setType(3);
		return bean;

	}

}
